package com.cug.nageoffer.user.controller;

import com.cug.nageoffer.model.entity.OfflineResume;
import com.cug.nageoffer.model.entity.OnlineResume;
import com.cug.nageoffer.model.entity.ResumeRecord;
import com.cug.nageoffer.model.vo.Result;
import com.cug.nageoffer.model.vo.ResultCodeEnum;
import com.cug.nageoffer.user.config.MinioConfiguration;
import com.cug.nageoffer.user.service.OfflineResumeService;
import com.cug.nageoffer.user.service.OnlineResumeService;
import com.cug.nageoffer.user.service.ResumeRecordService;
import com.cug.nageoffer.util.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@Tag(name = "用户简历相关")
@RestController
@RequestMapping("/api/seeker/user/resume")
public class ResumeController {

    @Resource
    private OnlineResumeService onlineResumeService;

    @Resource
    private OfflineResumeService offlineResumeService;

    @Resource
    private ResumeRecordService resumeRecordService;

    @Resource
    private MinioConfiguration minioConfiguration;

    /**
     * 用户提交/修改在线简历接口
     */
    @Operation(summary = "提交/修改在线简历")
    @PostMapping("/modifyOnline")
    public Result<String> modifyOnlineResume(@RequestBody OnlineResume requestParam){
        onlineResumeService.modify(requestParam);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户上传附件简历接口
     */
    @Operation(summary = "上传附件简历")
    @PostMapping("/uploadOffline")
    public Result<String> uploadOfflineResume(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        String filePath = minioConfiguration.putObject(multipartFile,"OfflineResume/");
        offlineResumeService.upload(filePath);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据用户id查询在线简历接口
     */
    @Operation(summary = "根据用户id查询在线简历")
    @PostMapping("/queryOnline")
    public Result<OnlineResume> queryOnlineResume(@RequestParam Long userId){
        log.info("根据用户id查询在线简历:{}",userId);

        return onlineResumeService.search(userId);
    }

    /**
     * 查询用户自己的在线简历
     */
    @Operation(summary = "查询用户自己的在线简历")
    @GetMapping("/querySelfOnline")
    public Result<OnlineResume> querySelfOnlineResume(){
        return onlineResumeService.search(AuthContextUtil.get().getId());
    }

    /**
     * 根据用户id查询附件简历接口
     */
    @Operation(summary = "根据用户id查询附件简历")
    @PostMapping("/queryOffline")
    public Result<List<OfflineResume>> queryOfflineResume(@RequestParam Long userId){
        return offlineResumeService.search(userId);
    }

    /**
     * 根据用户id查询附件简历接口
     */
    @Operation(summary = "查询用户自己的附件简历")
    @GetMapping("/querySelfOffline")
    public Result<List<OfflineResume>> querySelfOfflineResume(){
        return offlineResumeService.search(AuthContextUtil.get().getId());
    }

    /**
     * 根据文件名下载附件简历接口
     */
    @Operation(summary = "根据文件名下载附件简历")
    @PostMapping("/downloadOffline")
    public Result<String> downloadOfflineResume(@RequestParam("fileName")String fileName, HttpServletResponse response){
        minioConfiguration.download(fileName,response);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除附件简历
     */
    @Operation(summary = "删除附件简历")
    @PostMapping("/deleteOffline")
    public Result<String> deleteOfflineResume(@RequestParam Integer id){
        return offlineResumeService.delete(id);
    }

    /**
     * 用户投递简历
     */
    @Operation(summary = "用户投递简历")
    @PostMapping("/send")
    public Result<String> sendResume(@RequestBody ResumeRecord requestParam){
        return resumeRecordService.send(requestParam);
    }

    /**
     * 用户已投递简历计数
     */
    @Operation(summary = "用户已投递简历计数")
    @GetMapping("/count")
    public Result<Long> count(){
        return resumeRecordService.countRecord(AuthContextUtil.get().getId());
    }
}
